package com.authority.framework.web.controller;

import com.authority.framework.log.Log;
import com.authority.framework.log.enums.BusinessType;
import com.authority.framework.web.domain.SysOperLogEntity;
import com.authority.framework.web.service.SysOperLogService;
import com.authority.framework.base.util.laydata.LayDatas;
import com.authority.framework.base.util.laydata.PageUtils;
import com.authority.framework.base.util.laydata.ResultModel;
import com.authority.framework.base.util.laydata.ResultTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @program: authority
 * @description: 操作日志控制层
 * @author: Mr.Yan
 * @create: 2018-11-20 18:01
 **/
@Controller
@RequestMapping(value = "sysoperlogCon")
public class SysOperLogController {

    private static final Logger log = LoggerFactory.getLogger(SysOperLogController.class);

    @Autowired
    private SysOperLogService sysOperLogService;

    private Map<String, Object> paramMap;//参数

    private Map<String, Object> resultMap;//返回值

    /**
     * @Description: 获取操作日志展示数据
     * @Param:
     * @return:
     * @Author: Mr.Yan
     * @Date: 2018/12/15
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayDatas list(HttpServletRequest request, HttpServletResponse response,
            PageUtils pageUtils) {
        return sysOperLogService.pageLayInfo("system/SysOperLogSql", SysOperLogEntity.class, pageUtils);
    }

    /**
     * @Description: 删除多条操作日志
     * @Param:
     * @return:
     * @Author: Mr.Yan
     * @Date: 2018/12/27
     */

    @Log(title = "操作日志管理", businessType = BusinessType.DELETE)
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResultModel delete(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "operIds", required = false) String operIds) {
        try {
            String[] operIdArr = operIds.split(",");
            for (String operId : operIdArr) {
                Integer update = sysOperLogService.delete(SysOperLogEntity.class, Integer.valueOf(operId));
                if (update < 1) {
                    return ResultTools.result(404, "删除操作日志信息失败", null);
                }
            }
            return ResultTools.result(0, "", null);
        }
        catch (Exception e) {
            log.error("删除操作日志信息出现未知错误" + e.getMessage());
            return ResultTools.result(404, "删除操作日志信息出现未知错误", null);
        }
    }

    /**
     * @Description: 清空操作日志
     * @Param:
     * @return:
     * @Author: Mr.Yan
     * @Date: 2018/12/27
     */

    @Log(title = "操作日志管理", businessType = BusinessType.CLEAN)
    @ResponseBody
    @RequestMapping(value = "/deleteAll")
    public ResultModel deleteAll(HttpServletRequest request, HttpServletResponse response) {
        try {
            sysOperLogService.deleteAll();
            return ResultTools.result(0, "", null);
        }
        catch (Exception e) {
            log.error("删除操作日志信息出现未知错误" + e.getMessage());
            return ResultTools.result(404, "删除操作日志信息出现未知错误", null);
        }
    }
    /**
    * @Description: 操作日志查看打开页面之前获取数据
    * @Param:
    * @return:
    * @Author: Mr.Yan
    * @Date: 2018/12/27
    */
    @ResponseBody
    @RequestMapping(value = "/openOperLogDetail")
    public ModelAndView openOperLogDetail(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "operId", required = false) Integer operId) {
	    SysOperLogEntity operLogEntity = sysOperLogService.findByPrimaryKey(SysOperLogEntity.class, operId);
	    ModelAndView mv = new ModelAndView();
	    mv.setViewName("web/system/log/detail/operlog_detail");
	    mv.addObject("operlog", operLogEntity);
	    return mv;
    }

}
